!<template>
  <div class="search-popup">
    <!-- @input="onInput"   输入框内容变化时触发  onInput方法 -->
    <van-search
      v-model="searchValue"
      show-action
      :placeholder="placeholderVal"
      @search="onSearch"
      @cancel="onCancel"
      @input="onInput"
    />
    <!--      热门搜索，历史记录组件页面       父组件数据传子组件-->
    <HistoryHot
      v-if="blockShow == 1"
      :historyKeywordList="historyKeywordList"
      :hotKeywordList="hotKeywordList"
      @tagClick="tagClick"
    />

    <SearchTipsList
      v-if="blockShow == 2"
      :searchTipsListData="searchTipsListData"
      @cellClick="cellClick"
    />
    <SearchProducts
      v-if="blockShow == 3"
      :filterCategory="filterCategory"
      :goodsList="goodsList"
      @categoryChange="categoryChange"
      @priceChange="priceChange"
    />
  </div>
</template>
<script>
import { Toast } from "vant";
import HistoryHot from "@/components/HistoryHot";
import SearchTipsList from "@/components/SearchTipsList";
import SearchProducts from "@/components/SearchProducts";
import {
  GetSearchPopupData,
  GetSearchTipsListData,
  GetGoodsListData,
} from "@/request/api";

export default {
  components: {
    HistoryHot,
    SearchTipsList,
    SearchProducts,
  },
  props: {},
  data() {
    return {
      // 这里放数据
      searchValue: "",
      placeholderVal: "",
      blockShow: 1,
      // 历史记录的列表数据
      historyKeywordList: [],
      // 热门搜索的列表数据
      hotKeywordList: [],
      // 搜索实时提示的列表数据
      searchTipsListData: [],
      // 搜索商品，分类数据（综合，价格，分类）
      filterCategory: [],
      // 搜索到的商品列表数据
      goodsList: [],
      // 价格排序：（由高到低或者由低到高）
      order: "desc",
      // 分类id
      categoryId: 0,
      // 默认的搜索方式  是id 还是price
      sort: "id",
    };
  },
  // 声明周期  -  创建完成（可以访问当前this示例）
  created() {
    GetSearchPopupData().then((res) => {
      console.log("GetSearchPopupData......");
      //  console.log(res.data.defaultKeyword.keyword );
      console.log(res.data);
      this.placeholderVal = res.data.defaultKeyword.keyword;
      this.historyKeywordList = res.data.historyKeywordList;
      this.hotKeywordList = res.data.hotKeywordList;
    });
  },
  //   方法集合
  methods: {
    cellClick(itemVal){
      this.searchValue =itemVal;
       this.onSearch(this.searchValue);
    },
    tagClick(tagVal) {
      this.searchValue = tagVal;
      // 发送请求
      this.onSearch(this.searchValue);
    },
    priceChange(value) {
      this.order = value;
      this.sort = "price";
      // 发送请求
      this.onSearch(this.searchValue);
    },
    categoryChange(value) {
      this.categoryId = value;
      // 发送请求
      this.onSearch(this.searchValue);
    },
    onInput(val) {
      this.blockShow = 2;
      // console.log(val);
      GetSearchTipsListData({ keyword: val }).then((res) => {
        if (res.errno == 0) {
          console.log(res);
          this.searchTipsListData = res.data;
        }
      });
    },
    //搜索框输入内容后，按回车触发此事件
    onSearch(val) {
      this.blockShow = 3;
      GetGoodsListData({
        keyword: val,
        page: 1,
        size: 10,
        order: this.order,
        categoryId: this.categoryId,
        sort: this.sort,
      }).then((res) => {
        if (res.errno == 0) {
          // console.log(res);
          this.filterCategory = res.data.filterCategory;
          // console.log("处理前的this.filterCategory...");
          // console.log(JSON.stringify(this.filterCategory, null, 2));
          this.filterCategory = JSON.parse(
            JSON.stringify(this.filterCategory)
              .replace(/name/g, "text")
              .replace(/id/g, "value")
          );
          // this.filterCategory
          // console.log("处理后的this.filterCategory...");
          // console.log(JSON.stringify(this.filterCategory, null, 2));
          this.goodsList = res.data.goodsList;
          // this.searchTipsListData = res.data;
        }
      });
    },
    // 点击取消按钮触发此事件
    onCancel() {
      Toast("取消"); //Toast  是提示框。可以删掉，我们不需要
      // this.$router.push("/home")
      this.$router.go(-1);
    },
  },
};
</script>
<style>
.search-popup {
  width: 100%;
  height: 100%;
  position: absolute;
  /* right: 0;  删掉 */
  top: 0;
  /* background-color: rgba(0, 0, 0, 0.5); */
  background-color: #efefef;
}
</style>